package com.alogic.remote.httpclient;


import com.anysoft.util.Configurable;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.http.client.config.RequestConfig;

/**
 * 配置
 *
 * @since 1.6.13.21 [20201021 duanyy] <br>
 */
public class HttpClientConfig implements Configurable {
    /**
     * 是否输出指标
     */
    protected boolean metricsOutput = false;
    /**
     * 是否进入debug模式
     */
    protected boolean debug = false;
    /**
     * 是否开启gzip
     */
    protected boolean gzipEnable = false;
    /**
     * 编码
     */
    protected String encoding = "utf-8";
    /**
     * 重试次数
     */
    protected int autoRetryCnt = 2;

    protected RequestConfig requestConfig = null;

    protected boolean traceEnable = true;
    protected String traceId = "X-Trace-Id";
    protected String traceOrder = "X-Trace-Order";

    @Override
    public void configure(Properties p) {
        debug = PropertiesConstants.getBoolean(p,"http.debug",false,true);
        gzipEnable = PropertiesConstants.getBoolean(p,"rpc.http.gzip",false,true);
        encoding = PropertiesConstants.getString(p,"http.encoding",encoding);
        autoRetryCnt = PropertiesConstants.getInt(p, "rpc.ketty.autoRetryTimes", autoRetryCnt);
        metricsOutput = PropertiesConstants.getBoolean(p, "rpc.ketty.metricsOutput", metricsOutput);
        traceEnable = PropertiesConstants.getBoolean(p,"tracer.remote.enable",traceEnable);
        traceId = PropertiesConstants.getString(p,"http.traceId",traceId);
        traceOrder = PropertiesConstants.getString(p,"http.traceOrder",traceOrder);
    }

    public HttpClientConfig setMetricsOutput(boolean metricsOutput){
        this.metricsOutput = metricsOutput;
        return this;
    }

    public HttpClientConfig setGzipEnable(boolean gzipEnable){
        this.gzipEnable = gzipEnable;
        return this;
    }

    public HttpClientConfig setEncoding(String encoding){
        this.encoding = encoding;
        return this;
    }

    public HttpClientConfig setAutoRetryCnt(int autoRetryCnt){
        this.autoRetryCnt = autoRetryCnt;
        return this;
    }

    public HttpClientConfig setRequestConfig(RequestConfig config){
        this.requestConfig = config;
        return this;
    }

    public HttpClientConfig setTraceEnable(boolean traceEnable){
        this.traceEnable = traceEnable;
        return this;
    }

    public HttpClientConfig setTraceId(String traceId){
        this.traceId = traceId;
        return this;
    }

    public HttpClientConfig setTraceOrder(String traceOrder){
        this.traceOrder = traceOrder;
        return this;
    }

    public boolean isMetricsOutput(){
        return this.metricsOutput;
    }

    public boolean isDebug(){
        return this.debug;
    }

    public boolean isGzipEnable(){
        return this.gzipEnable;
    }

    public String getEncoding(){
        return encoding;
    }

    public int getAutoRetryCnt(){
        return this.autoRetryCnt;
    }

    public boolean isTraceEnable(){
        return traceEnable;
    }

    public String getTraceId(){
        return traceId;
    }

    public String getTraceOrder() {
        return traceOrder;
    }

    public RequestConfig getRequestConfig(){
        if (this.requestConfig == null){
            this.requestConfig = RequestConfig.custom().build();
        }
        return this.requestConfig;
    }
}
